const mysql = require("mysql2")

module.exports = class Model {
  // 连接对象
  static conn = null

  // 连接数据库
  static connect() {
    Model.conn = mysql.createConnection({
      host: "47.92.83.132",
      user: "root",
      password: "swt@2023",
      database: "ozon",
    })
    // Model.conn = mysql.createConnection({
    //   host: "127.0.0.1",
    //   user: "root",
    //   password: "liucong789AINI",
    //   database: "ozon",
    // })
    Model.conn.connect((err) => {
      if (err) {
        console.log("数据库连接失败")
        // console.log(err)
      }
    })
  }
  // 关闭连接
  static close() {
    if (Model.conn != null) {
      Model.conn.end()
    }
  }
  //
  static query(sql, params = []) {
    return new Promise((resolve, reject) => {
      this.connect()
      console.log(sql)
      Model.conn.query(sql, params, (err, res) => {
        console.log(err)
        if (err) {
          reject(err)
        } else {
          resolve(res)
        }
      })
      this.close()
    })
  }
}
